// pages/confirmorder/confirmorder.js
let network = require("../../utils/network.js")
let app = getApp();
let url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList: '',
    goodslist: [],
    num: 1,
    discount: 0,
    options: [],
    moren: [],//默认,
    zt: true,
    price: 0,//总价
    baoyou: '',//是否包邮,
    goods_id:null,
    goods_ids:[],
    goods_nums:[],
    shopcart:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 商品信息
    console.log(options)
    var that = this
    if (options.show == 'detail'){
      that.setData({
        num:options.num,
        shopcart:2
      })
      that.deta(options.id)
      // 单个
    } 
    if (options.show == 'cart'){
       that.info()
      that.setData({
        shopcart: 1
      })
       // 多个
    }
      that.setData({
        show: options.show,
        discount: wx.getStorageSync('discount')
      }) 
  },
  // 默认
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  deta(e,num){
    var that =this
    network.request({
      url: url + '/AppletsApi/Goods/goodsDetail.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
        goods_id: e,
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        var arr = []
        var num = that.data.num
        var discount = that.data.discount
        var data = data.data.data
        
        arr.push(data)
        that.setData({
          goodslist: arr,
          price: arr[0].goods_price * num * (discount / 10),
          baoyou: arr[0].goods_freight == '0.00' ? '包邮':arr[0].goods_freight,
          goods_id: arr[0].id,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.orderList()
  },

  info(){
    var that = this
    network.request({
      url: url + '/AppletsApi/Goods/getShopCartChangeInfo.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        var discount = that.data.discount
        var data = data.data.data
        var num = 0
        var aa = []
        var bb = []
        for (var i = 0; i < data.length;i++){
          var price = data[i].goods_price * data[i].goods_num * (discount / 10)
          num += parseInt(data[i].goods_num)
          aa.push(data[i].goods_id)
          bb.push(data[i].goods_num)
        }
        console.log(aa)
        that.setData({
          goodslist: data,
          num: num,
          price: price,
          goods_ids: aa,
          goods_nums:bb         
        })
      }
    })
  },
  
  orderList() {
    var that = this
    if (app.globalData.address) {
      that.setData({
        addressList: app.globalData.address
      })
      return false
    }
    network.request({
      url: url + '/AppletsApi/Address/getAddressDefault.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        that.setData({
          addressList: data.data.data
        })
      }
    })
  },


  ddsc:function(){
    var that = this
    network.request({
      url: url + '/AppletsApi/Order/orderAdd.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
        addres_id: that.data.addressList.id,
        goods_id: that.data.goods_id,
        goods_num: that.data.num,
        ShopCart: that.data.shopcart
      },
      method: 'post',
      header: {
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
      },
      success: function (data) {
        console.log(data)
        network.request({
          url: url + '/AppletsApi/Order/orderWechatPay.html',
          data: {
            applet_openid: wx.getStorageSync('applet_openid'),
            out_trade_no: data.data.out_trade_no,
            paytype: 1,
          },
          method: 'post',
          header: {
            "content-type": "application/x-www-form-urlencoded"
          },
          success: function (data) {
            // 调用支付接口
            wx.requestPayment({
              timeStamp: data.data.timeStamp,
              nonceStr: data.data.nonceStr,
              package: data.data.package,
              signType: data.data.signType,
              paySign: data.data.paySign,
              success(res) {
                wx.navigateTo({
                  url: '/pages/myorder/myorder?state=2',
                })
              },
              fail(res) {    
                wx.navigateTo({
                  url: '/pages/myorder/myorder?state=1',
                })
              }
            })

          }
        })
      }
    })
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
      
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  collectsite: function (e) {
    var that = this
    wx.navigateTo({
      url: '/pages/collectsite/collectsite',
    })
  },
})